package com.yegs.jindie.util;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtil {


    private static final String BASE_PACKAGE = "D:/excel/";

    public static void exporExcel(String filename, Class cla, List<? extends BaseRowModel> list) throws IOException {
        String path = BASE_PACKAGE + filename +ExcelTypeEnum.XLSX.getValue();
        FileOutputStream fio = new FileOutputStream(path);
        ExcelWriter writer = null;
        try {
            //实例化 ExcelWriter
            writer = new ExcelWriter(fio, ExcelTypeEnum.XLSX, true);
            //实例化表单
            Sheet sheet = new Sheet(1, 0, cla);
            sheet.setSheetName(filename);
            //输出
            writer.write(list, sheet);
            writer.finish();
            fio.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fio.close();
        }
    }

    public static String exporExcelByFtp(String filename, Class cla, List<? extends BaseRowModel> list) throws IOException {
        String path = filename + ExcelTypeEnum.XLSX.getValue();
        FileOutputStream fio = new FileOutputStream(path);
        ExcelWriter writer = null;
        try {
            //实例化 ExcelWriter
            writer = new ExcelWriter(fio, ExcelTypeEnum.XLSX, true);
            //实例化表单
            Sheet sheet = new Sheet(1, 0, cla);
            sheet.setSheetName(filename);
            //输出
            writer.write(list, sheet);
            writer.finish();
            fio.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fio.close();
        }
        return path;

    }
}
